package cn.linjianhui.thread.volatiles;

public class MultiThreadLong {
    public static long t = 0;

    public static class ChangeT implements Runnable {

        private long val;

        public ChangeT(long val) {
            this.val = val;
        }

        @Override
        public void run() {
            while (true) {
                MultiThreadLong.t = val;
                Thread.yield();
            }
        }
    }

    public static class ReadT implements Runnable {

        @Override
        public void run() {
            while (true) {
                long tmp = t;
                if(tmp != 111L && tmp != -999L && tmp != 333L && tmp != -444L) {
                    System.out.println(tmp);
                }
                Thread.yield();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        new Thread(new ChangeT(111L)).start();
        new Thread(new ChangeT(-999L)).start();
        new Thread(new ChangeT(333L)).start();
        new Thread(new ChangeT(-444L)).start();

        new Thread(new ReadT()).start();
    }
}
